package com.compmov.webservice;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.compmov.datamodel.Comentario;
import com.compmov.datamodel.Lugar;
import com.compmov.datamodel.Persona;
import com.compmov.libraries.QueryParamDictionary;
import com.google.gson.Gson;


public class ComentarioWSClient {
	
	private WebServiceClient webService;
	private static final String TAG = "WEBSERVICE";
	
	
	public ComentarioWSClient(String WSName) {
		super();
		webService = new WebServiceClient("/rest/"+WSName);
	}


	public ComentarioWSClient(WebServiceClient webService) {
		super();
		this.webService = webService;
	}
	
	public void addComentario(Comentario comentario) throws WSClientException {				
		Gson gson = new Gson();
		// Pass the parameters if needed , if not then pass dummy one as follows
		Map<String, String> params = new HashMap<String, String>();
		String jsonCad = new Gson().toJson(comentario,Comentario.class);
		params.put(QueryParamDictionary.COMENTARIO_PARAM, jsonCad);
		
		Log.d(TAG, "INVOCANDO GET COMENTARIO");
		String response = webService.webPut("/insertComments", params);
		Log.d(TAG, "RESPONSE = " + response);		
	}
	
	public List<Comentario> getComentarios(int id_lugar) throws WSClientException {		
		String response;
		Gson gson = new Gson();
		JSONObject jsonObj;
		JSONArray jsonComments;
		JSONObject comment;
		Comentario comment2;
		List<Comentario> comments = new LinkedList<Comentario>();
		
		Map<String,String> params = new HashMap<String,String>();
		Log.d(TAG,"Iniciando WEBSERVICE...");
		params.put("id_lugar", String.valueOf(id_lugar));
		
		response = webService.webGet("/getComments", params);
		Log.d(TAG,"Response:"+response);
		try {
			WSError error = gson.fromJson(response, WSError.class);
			if (error.getError_description() == null){
				try {
					jsonObj = new JSONObject(response);
					jsonComments = jsonObj.optJSONArray("comentario");
					if(jsonComments != null){
						// Si hay más de un objeto en response.
						for(int i=0; i<jsonComments.length();i++){
							comment = jsonComments.getJSONObject(i);
							// Parsea de la cadena json al objeto Comentario
							comment2 = gson.fromJson(comment.toString(), 
									Comentario.class);
							comment2.getPersona().setFotografia(WebServiceClient.getServerURL()
									+comment2.getPersona().getFotografia());
							comments.add(comment2);
						}
					}else{
						comment = jsonObj.getJSONObject("comentario");						
						comment2 = gson.fromJson(comment.toString(), 
								Comentario.class);
						if (comment2.getId()!=0){
						comment2.getPersona().setFotografia(WebServiceClient.getServerURL()
								+comment2.getPersona().getFotografia());
						comments.add(comment2);
						}						
					}
					Log.d(TAG, "Comentarios: "+comments);
					return comments;
				} catch (JSONException e) {
					Log.e(TAG, e.getMessage(), e);
					throw new WSClientException(e);
					// TODO: handle exception
				}
			}else{
				Log.e(TAG, error.getError_description()+"");
				throw new WSClientException(error);
			}
		} catch (Exception e) {
			// TODO: handle exception
			Log.e(TAG, e.getMessage(), e);
			throw new WSClientException(e);
		}				
	}		

	
}
